﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using QxFramework.Core;
using System;

public class DataModule<T> : LogicModuleBase, IDataManager,IDataModule<T> where T:GameDataBase,new()
{
    private T _mainData;
    public T GameData
    {
        get
        {
            return _mainData;
        }
    }
    private string _fileName;
    private DataType _dataType;
    public string UserName {
        get
        {
            return _fileName;
        }
    }
    /// <summary>
    /// args 为用户名称
    /// </summary>
    /// <param name="args"></param>
    public override void Init(object args)
    {

        Debug.Assert(string.IsNullOrEmpty((args as DataTypePair).fileName )== false);
        _fileName = (args as DataTypePair).fileName+".json";
        _dataType = (args as DataTypePair).dataType;
        if (!RegisterData<T>(out _mainData))
        {
            InitMainData();
        }
    }
    protected bool RegisterData<T>(out T data) where T : GameDataBase, new()
    {
        return Data.Instance.InitData<T>(out data, _dataType);
    }

    protected void SetModify<T>(T data) where T : GameDataBase, new()
    {
        Data.Instance.SetModify<T>(data, this, _dataType);
    }

    public override void Update()
    {
        base.Update();
    }

    public virtual void InitMainData()
    {

    }

   /* private string ReadFromCSV()
    {
        return Data.Instance.TableAgent.GetString("Test","1","Value");
    }
   */
    public void LoadFrom()
    {
        Data.Instance.LoadFromFile();
    }

    public void SaveTo()
    {
        Data.Instance.SaveToFile();
    }

    public void RefreshData()
    {
        SetModify<T>(_mainData);
    }
}
public interface IDataModule<T>
{
    T GameData { get; }
}
public interface IDataManager
{
    void LoadFrom();
    void SaveTo();

    string UserName
    {
        get;
    }
    void RefreshData();
}
public enum DataType
{
    GlobalSetting,
    LevelItemsInfo
}
public class DataTypePair
{
    public string fileName;
    public DataType dataType;
}